using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class JobSeekers_jobalerts : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
                     WebConfigurationManager.ConnectionStrings[
                       "RecruitVietnam"].ConnectionString;
        if (Session.Contents["jsuserid"] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (!IsPostBack)
            ShowData();

    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("jobalert.aspx");
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string alertID = "";
        foreach (GridViewRow gridViewRow in GridView1.Rows)
        {
            CheckBox checkBox = (CheckBox)
            gridViewRow.FindControl("chkAlertID");
            if (checkBox.Checked)
            {
                try
                {
                    alertID +=
                    gridViewRow.Cells[1].Text + ",";
                }
                catch (Exception ex)
                {
                    literalError.Text = ex.Message;
                }
            }

        }
        if (alertID != "")
        {
            alertID = alertID.Substring(0, alertID.Length - 1);
            RecruitVietnam recruitVietnam
                        = new RecruitVietnam();
            recruitVietnam.ConnectionString = connectionString;
            foreach (string alert in alertID.Split(','))
                recruitVietnam.DeleteJobAlert(
                Convert.ToString(Session["jsuserid"]), alert);
            ShowData();
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        ShowData();
        GridView1.PageIndex = e.NewPageIndex;
    }
    void ShowData()
    {
        try
        {
            SqlServerProvider cls = new SqlServerProvider(connectionString);
            cls.CommandText = "SPViewJobAlert";
            cls.ParameterCollection = new string[1] { "@UserID" };
            cls.ValueCollection = new string[1] { 
            Convert.ToString(Session.Contents["jsuserid"]) };
            cls.CommandType = CommandType.StoredProcedure;
            DataTable dataTable = cls.GetDataTable();
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataTableToGridView(dataTable, this.GridView1, 10, 10, PagerButtons.NumericFirstLast);

            this.GridView1.HeaderRow.Cells[8].Visible = false;
            MakeUrlForImage();
            if (GridView1.Rows.Count > 1)
            {
                btnDelete.Enabled = true;                
            }
            else
            {               
                btnDelete.Enabled = false;
            }
             
            if (dataTable.Rows.Count >= 5)
                btnNew.Enabled = false;
            else
                btnNew.Enabled = true;
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gridViewRow = GridView1.Rows[e.RowIndex];
        string alertID = gridViewRow.Cells[1].Text;
        string status = gridViewRow.Cells[9].Text;
        if (alertID != "")
            if (ActivateAlert(alertID, status))
            {
                ShowData();
            }
    }
    void MakeUrlForImage()
    {
        foreach (GridViewRow gridViewRow in GridView1.Rows)
        {
            string activate = gridViewRow.Cells[9].Text;

            ImageButton imageButton;

            imageButton = (ImageButton)
            gridViewRow.Cells[8].FindControl("ibnActviate");            
            if (gridViewRow.Cells[9].Text == "False")
            {                
                imageButton.ImageUrl = "/icons/lock.gif";

            }
            gridViewRow.Cells[9].Visible = false;
        }
    }
    bool ActivateAlert(string alertID, string status)
    {
        bool update = false;
        try
        {
            SqlServerProvider cls = new SqlServerProvider(connectionString);
            cls.CommandText = "Update tblJSJobAlerts";
            cls.CommandText += ((status == "True")?" set Activate=0 ":" set Activate=1");
             cls.CommandText += " where UserID='" + Convert.ToString(
            Session.Contents["jsuserid"]) + "' and AlertID ='" + alertID + "'";
            update = (cls.ExecuteNonQuery() > 0);
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
        return update;
    }
     
}
